# coding=utf-8
__author__ = 'liubin'

from project.models import User, Role, MenuRole
from project.tools import Session
from project.settings import menu
if __name__ == '__main__':
    # 数据库初始化
    session = Session()
    #with session.no_autoflush:
        # 初始化权限表
    admin = session.query(Role).filter_by(code='admin', isDeleted=False).first()
    if not admin:
        admin =  Role()
        admin.code = 'ADMIN'
        admin.name = u'管理员'
        session.add(admin)
        print(admin.id)
    user = session.query(User).filter_by(userId='admin', isDeleted=False).first()
    # 初始化用户表
    if not user:
        user = User()
        user.userId = 'admin'
        user.name = u'管理员'
        user.set_encrypt_pwd('admin')
        user.roleId = admin.id
        session.add(user)
        #print(user.userId)
    # 初始化菜单表
    session.query(MenuRole).filter_by(roleId=admin.id).delete()
    for item in menu:
        mr = MenuRole()
        mr.roleId = admin.id
        mr.menu = item
        mr.enabled = True
        session.add(mr)

    session.commit()
